

//模拟发送http请求
var request = require("request");
var fs = require('fs');
var qs = require('querystring');

// 开始抽取数据
getList();

function getList (num = 21092) {
  var param = {
    sortBy: 'rank',
    pageNumber: num,
    pageSize: 1,
    author: 'all'
  }
  console.log(`获取第${num}个数据开始 -----------------------------------`);
  var url = `https://www.makeapie.com/chart/list?${qs.stringify(param)}`;
  request(url, function (error, response, body) {
    try {
      var data = JSON.parse(response.body).data.charts[0];
      getInfo(data.cid, num).then(() => {
        getList(num + 1);
        // if (num < 3) {
        //   getList(num + 1);
        // }
      });
    } catch (err) {
      addLogs(`获取第${num}个数据失败，接口：${url}`);
    }
  });
}


function getInfo (cid, pageNumber) {
  return new Promise((resolve, reject) => {
    var url = `https://www.makeapie.com/chart/get/${cid}`;
    console.log(`获取 ${cid} 详情开始`);
    request(url, function (error, response, body) {
      try {
        var data = JSON.parse(response.body).data;
        // 添加数据
        appendInfo(data, pageNumber).then(() => {
          resolve();
        }).catch(() => {
          reject();
        });
        resolve();
      } catch (err) {
        addLogs(`获取详情失败，失败位置pageNumber=${pageNumber}，失败接口：${url}`);
        reject();
      }
    });
  })
}

// 添加数据
function appendInfo (data, pageNumber) {
  return new Promise((resolve, reject) => {
    createTxt(data, pageNumber).then(() => {
      var info = {
        c: data.cid,
        l: new Date(data.lastUpdateTime).getTime(),
        s: data.starCount,
        v: data.viewCount,
        b: data.builtinTags
      }
      var time = new Date(data.lastUpdateTime).getFullYear();
      fs.appendFile(__dirname + `/echarts/index/${time}.txt`, JSON.stringify(info) + '\n', function (err) {
        if (err) {
          addLogs(`插入索引失败，失败位置pageNumber=${pageNumber}，cid=${data.cid}`);
          reject();
        } else {
          console.log('--- 完成 ---');
          resolve();
        }
      })
    }).catch(() => {
      reject();
    })
  })
}


// 创建文件
function createTxt (data, pageNumber) {
  return new Promise((resolve, reject) => {
    var pathName = __dirname + '/echarts/data/' + data.cid + ".txt";

    // 判断该文件是否存在
    fs.exists(pathName, function (res) {
      if (!res) {
        // JSON.stringify(data).replace(/\\n/g, '\n')
        fs.writeFile(pathName, JSON.stringify(data), function(err) {
          if(err) {
            addLogs(`保存数据失败，失败位置pageNumber=${pageNumber}，cid=${data.cid}`);
          }
        });
        // 下载图片
        downImg(data, pageNumber);
        resolve();
      } else {
        addLogs(`文件已存在，不重复添加：${data.cid}`);
        reject();
      }
    })
  })
}

// 下载并保存图片
function downImg (data, pageNumber) {
  var writeStream = fs.createWriteStream(__dirname + `/echarts/image/${data.cid}.png`,{autoClose:true});
  request(data.thumbnailURL).pipe(writeStream);

  writeStream.on('error',function(){
    addLogs(`图片写入失败，失败位置pageNumber=${pageNumber}，cid=${data.cid}，失败接口：${data.thumbnailURL}`)
  })
}

// 添加错误日志
function addLogs (msg) {
  console.log(msg);
  msg = new Date().toLocaleString() + ' ' + msg + '\n';
  fs.appendFile(__dirname + '/logs/error.log', msg, function(err) {
    if(err) {
      return console.log(err);
    }
  });
}

// 补0
function makeUpNum (num) {
  return num < 10 ? '0' + num : num;
}
















// fs.readFile('./echarts/index.txt', function (err, data) {
//    if (err) {
//        return console.error(err);
//    }
//    console.log(typeof data.toString().split('\\n'));
// });



